#include "tcp_socket.hpp"
#define CHECE_ERS(q) if((q)==false){return -1;}

int main()
{
  TcpSocket list_sock;
  //创建套接字
  CHECE_ERS(list_sock.Socket());
  //绑定地址信息
  CHECE_ERS(list_sock.Bind("192.168.227.145",9000));
  //开始监听
  CHECE_ERS(list_sock.Listen());

  //获取新建链接
  while(1)
  {
    TcpSocket conn_sock;
    std::string cliip;
    std::string cliprot;
    bool ret;
    ret=list_sock.Accept(&conn_sock,&cliip,&cliprot);
    if(ret==false)
    {
      continue;
    }

    std::cout<<"new connection"<<cliip<<":"<<cliprot<<std::endl;


   //新建连接与客户端通信
    std::string buf;

    ret=conn_sock.Recv(&buf);
    if(ret==false)
    {
      conn_sock.Close();
      continue;
    }
    std::cout<<"client say:"<<buf;

    std::cout<<"serve say:";

    fflush(stdout);
    std::cin>>buf;
    ret=conn_sock.Send(buf);
    if(ret==false)
    {
      conn_sock.Close();
      continue;
    }
  }
  //关闭套接字
  list_sock.Close();
  return 0;
}






